package com.softcloud.comingforyuan.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.softcloud.comingforyuan.model.Exercise;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 喆歆 on 2015/6/30.
 */
public class ExerciseDB {

    public static final String DB_NAME = "exercise.db";

    public static final int VERSION = 1;

    private static ExerciseDB exerciseDB;

    private SQLiteDatabase db;

    private ExerciseDB(Context context){
        Log.d("debug","DB构造函数执行");

        ExerciseOpenHelper dbHelper = new ExerciseOpenHelper(context,DB_NAME,null,VERSION);
        db = dbHelper.getWritableDatabase();
    }

    public synchronized static ExerciseDB getInstance(Context context){
        Log.d("debug","DB_getInstancce");

        if(exerciseDB == null){
            exerciseDB = new ExerciseDB(context);
        }
        return exerciseDB;
    }

    public List<Exercise> loadAllExercise(){
        List<Exercise> list = new ArrayList<Exercise>();
        Cursor cursor = db.query("Exercise",null,null,null,null,null,null);
        if(cursor.moveToFirst()){
            do{
                Exercise exercise = new Exercise();
                exercise.setId(cursor.getInt(cursor.getColumnIndex("id")));
                exercise.setQuestion(cursor.getString(cursor.getColumnIndex("question")));
                exercise.setOptionA(cursor.getString(cursor.getColumnIndex("optionA")));
                exercise.setOptionB(cursor.getString(cursor.getColumnIndex("optionB")));
                exercise.setOptionC(cursor.getString(cursor.getColumnIndex("optionC")));
                exercise.setOptionD(cursor.getString(cursor.getColumnIndex("optionD")));
                exercise.setRightAnswer(cursor.getInt(cursor.getColumnIndex("right_answer")));
                exercise.setCurrAnswer(cursor.getInt(cursor.getColumnIndex("current_answer")));
                exercise.setAnalyses(cursor.getString(cursor.getColumnIndex("analyses")));
                list.add(exercise);
            }while (cursor.moveToNext());
        }
        if(cursor != null){
            cursor.close();
        }
        return list;
    }

    public List<Exercise> loadWrongExercise(){
        List<Exercise> list = new ArrayList<Exercise>();
        Cursor cursor = db.rawQuery("select * from exercise where right_answer<>current_answer",null);
        if(cursor.moveToFirst()){
            do{
                Exercise exercise = new Exercise();
                exercise.setId(cursor.getInt(cursor.getColumnIndex("id")));
                exercise.setQuestion(cursor.getString(cursor.getColumnIndex("question")));
                exercise.setOptionA(cursor.getString(cursor.getColumnIndex("optionA")));
                exercise.setOptionB(cursor.getString(cursor.getColumnIndex("optionB")));
                exercise.setOptionC(cursor.getString(cursor.getColumnIndex("optionC")));
                exercise.setOptionD(cursor.getString(cursor.getColumnIndex("optionD")));
                exercise.setRightAnswer(cursor.getInt(cursor.getColumnIndex("right_answer")));
                exercise.setCurrAnswer(cursor.getInt(cursor.getColumnIndex("current_answer")));
                exercise.setAnalyses(cursor.getString(cursor.getColumnIndex("analyses")));
                list.add(exercise);
            }while (cursor.moveToNext());
        }
        if(cursor != null){
            cursor.close();
        }
        return list;
    }

    public void setCurrAnswer(int currId,int currAnswer){
        String idStr = Integer.toString(currId);
        ContentValues values = new ContentValues();
        values.put("current_answer",currAnswer);
        db.update("Exercise",values,"id = ?",new String[]{idStr});
    }
    public int getCurrAnswer(int currId){
        int res = 0;
        String idStr = Integer.toString(currId);
        Cursor cursor = db.query("Exercise",new String[]{"current_answer"},"id = ?",new String[]{idStr},null,null,null);
        if(cursor.moveToFirst()){
            res = cursor.getInt(cursor.getColumnIndex("current_answer"));
        }
        cursor.close();
        return res;
    }
}
